﻿namespace com.stifel.ServerBuildMgmtDao
{
    using System;
    using System.Linq;

    using Microsoft.SharePoint.Linq;

    using Context = StifelServerBuildDataContext;

    using CustomObject = StifelVirtualServerRequest;

    public class StifelServerBuildDao : StifelBaseDao
    {
        #region Fields

        public const string ListName = "stifelvirtualserverrequest";

        #endregion Fields

        #region Constructors

        public StifelServerBuildDao( )
            : base(ListName)
        {
            //CurrentWebUrl = SPContext.Current.Web.Url;
            CurrentWebUrl = ConfiguredWebUrl;
        }

        public StifelServerBuildDao( string requestUrl )
            : base(ListName)
        {
            this.CurrentWebUrl = requestUrl;
        }

        #endregion Constructors

        #region Methods

        public CustomObject Get( Guid uniqueId )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                CustomObject v = ( from o in l
                                                 where o.UniqueId == uniqueId
                                                 select o ).SingleOrDefault( );

                return v;
            }
        }

        public CustomObject GetForADBuild( Guid id )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                CustomObject v = ( from o in l
                                                 where o.UniqueId == id &&
                                                 ( o.ADConfigStatus == ADConfigStatus.Available ||
                                                 o.ADConfigStatus == ADConfigStatus.Started )
                                                 select o ).SingleOrDefault( );

                return v;
            }
        }

        public CustomObject GetForADInfo( Guid id )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                CustomObject v = ( from o in l
                                                 where o.UniqueId == id &&
                                                 ( o.ADConfigStatus == ADConfigStatus.Available ||
                                                 o.ADConfigStatus == ADConfigStatus.Started )
                                                 select o ).SingleOrDefault( );

                return v;
            }
        }

        public CustomObject GetForAntiVirusBuild( Guid id )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                CustomObject v = ( from o in l
                                                 where o.UniqueId == id &&
                                                 ( o.AVConfigStatus == AVConfigStatus.Available ||
                                                 o.AVConfigStatus == AVConfigStatus.Started )
                                                 select o ).SingleOrDefault( );

                return v;
            }
        }

        public CustomObject GetForAntiVirusInfo( Guid id )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                CustomObject v = ( from o in l
                                                 where o.UniqueId == id &&
                                                 ( o.AVConfigStatus == AVConfigStatus.Available ||
                                                 o.AVConfigStatus == AVConfigStatus.Started )
                                                 select o ).SingleOrDefault( );

                return v;
            }
        }

        public CustomObject GetForBackupBuild( Guid id )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                CustomObject v = ( from o in l
                                                 where o.UniqueId == id &&
                                                 ( o.BackupConfigStatus == BackupConfigStatus.Available ||
                                                 o.BackupConfigStatus == BackupConfigStatus.Started )
                                                 select o ).SingleOrDefault( );

                return v;
            }
        }

        public CustomObject GetForBackupInfo( Guid id )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                CustomObject v = ( from o in l
                                                 where o.UniqueId == id &&
                                                 ( o.BackupConfigStatus == BackupConfigStatus.Available ||
                                                 o.BackupConfigStatus == BackupConfigStatus.Started )
                                                 select o ).SingleOrDefault( );

                return v;
            }
        }

        public CustomObject GetForDeliveryBuild( Guid id )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                CustomObject v = ( from o in l
                                                 where o.UniqueId == id &&
                                                 ( o.BuildStatus == BuildStatus.Available ||
                                                 o.BuildStatus == BuildStatus.Started )
                                                 select o ).SingleOrDefault( );

                return v;
            }
        }

        public CustomObject GetForDeliveryInfo( Guid id )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                CustomObject v = ( from o in l
                                                 where o.UniqueId == id &&
                                                 ( o.BuildStatus == BuildStatus.Available ||
                                                 o.BuildStatus == BuildStatus.Started )
                                                 select o ).SingleOrDefault( );

                return v;
            }
        }

        public CustomObject GetForLeadInfo( Guid id )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                CustomObject v = ( from o in l
                                                 where o.UniqueId == id &&
                                                 ( o.ApprovalStatus == ApprovalStatus.Available ||
                                                 o.ApprovalStatus == ApprovalStatus.Started )
                                                 select o ).SingleOrDefault( );

                return v;
            }
        }

        public CustomObject GetForMonitorBuild( Guid id )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                CustomObject v = ( from o in l
                                                 where o.UniqueId == id &&
                                                 ( o.MonitorConfigStatus == MonitorConfigStatus.Available ||
                                                 o.MonitorConfigStatus == MonitorConfigStatus.Started )
                                                 select o ).SingleOrDefault( );

                return v;
            }
        }

        public CustomObject GetForMonitorInfo( Guid id )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                CustomObject v = ( from o in l
                                                 where o.UniqueId == id &&
                                                 ( o.MonitorConfigStatus == MonitorConfigStatus.Available ||
                                                 o.MonitorConfigStatus == MonitorConfigStatus.Started )
                                                 select o ).SingleOrDefault( );

                return v;
            }
        }

        public CustomObject GetForRequest( Guid id )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                CustomObject v = ( from o in l
                                                 where o.UniqueId == id &&
                                                 ( o.RequestorStatus == RequestorStatus.Available ||
                                                 o.RequestorStatus == RequestorStatus.Started )
                                                 select o ).SingleOrDefault( );

                return v;
            }
        }

        public CustomObject getRequestFromListUsingGUID( Guid value )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                CustomObject v = ( from o in l
                                                 where o.UniqueId == value
                                                 select o ).SingleOrDefault( );
                return ( v );
            }
        }

        public CustomObject Insert( CustomObject value )
        {
            Guid guid = Guid.NewGuid( );
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                l.InsertOnSubmit( new CustomObject
                {
                    ADConfigStatus = ADConfigStatus.NA,
                    AVConfigStatus = AVConfigStatus.NA,
                    BackupConfigStatus = BackupConfigStatus.NA,
                    BuildStatus = BuildStatus.NA,
                    ApprovalStatus = ApprovalStatus.NA,
                    MonitorConfigStatus = MonitorConfigStatus.NA,
                    PatchConfigStatus = PatchConfigStatus.NA,
                    SerializedVirtualServerRequest = string.Empty,
                    OverallStatus = OverallStatus.Started,
                    RequestorStatus = RequestorStatus.NA,
                    Title = guid.ToString( )
                } );
                c.SubmitChanges( );
                CustomObject v = ( from o in l
                                                 where o.Title == guid.ToString( )
                                                 select o ).SingleOrDefault( );
                v.SerializedVirtualServerRequest = value.SerializedVirtualServerRequest;
                v.RequestorStatus = value.RequestorStatus;
                v.Control = value.Control;
                v.Title = v.Id.ToString( );
                c.SubmitChanges( );

                return v;
            }
        }

        public CustomObject Update( CustomObject value )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                CustomObject v = ( from o in l
                                                 where o.UniqueId == value.UniqueId
                                                 select o ).SingleOrDefault( );
                if ( v != null )
                {
                    v.ActivityAuditLog = value.ActivityAuditLog;
                    v.ADConfigNotificationDate = value.ADConfigNotificationDate;
                    v.ADConfigStatus = value.ADConfigStatus;
                    v.ApprovalNotificationDate = value.ApprovalNotificationDate;
                    v.ApprovalStatus = value.ApprovalStatus;
                    v.AVConfigNotificationDate = value.AVConfigNotificationDate;
                    v.AVConfigStatus = value.AVConfigStatus;
                    v.BackupConfigNotificationDate = value.BackupConfigNotificationDate;
                    v.BackupConfigStatus = value.BackupConfigStatus;
                    v.BuildNotificationDate = value.BuildNotificationDate;
                    v.BuildStatus = value.BuildStatus;
                    v.Control = value.Control;
                    v.MonitorConfigNotificationDate = value.MonitorConfigNotificationDate;
                    v.MonitorConfigStatus = value.MonitorConfigStatus;
                    v.NotificationId = value.NotificationId;
                    v.OverallStatus = value.OverallStatus;
                    v.PatchConfigNotificationDate = value.PatchConfigNotificationDate;
                    v.PatchConfigStatus = value.PatchConfigStatus;
                    v.RequestApproved = value.RequestApproved;
                    v.RequestCanceled = value.RequestCanceled;
                    v.RequestCompleted = value.RequestCompleted;
                    v.RequestorNotificationDate = value.RequestorNotificationDate;
                    v.SerializedVirtualServerRequest = value.SerializedVirtualServerRequest;
                    v.RequestRejected = value.RequestRejected;
                    v.RequestorStatus = value.RequestorStatus;
                    v.Title = value.Id.ToString( );
                    // commit changes
                    c.SubmitChanges( );
                    return v;
                }
                else
                    return null;
            }
        }

        public CustomObject Upsert( CustomObject value )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                CustomObject v = ( from o in l
                                                 where o.UniqueId == value.UniqueId
                                                 select o ).SingleOrDefault( );
                if ( v == null )
                    return Insert( value );
                else
                    return Update( value );
            }
        }

        #endregion Methods
    }
}